package com.ddshuai.easy;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ddshuai
 * @version 1.0.0
 * @date 2018/3/19
 * @modify desc
 * @2018/3/19 新增        1.0.0
 * <p>
 * 给定一个整数数列，找出其中和为特定值的那两个数。
 * 你可以假设每个输入都只会有一种答案，同样的元素不能被重用。
 * 给定 nums = [2, 7, 11, 15], target = 9
 * <p>
 * 因为 nums[0] + nums[1] = 2 + 7 = 9
 * 所以返回 [0, 1]
 */
public class TwoNumSum {
    /**
     * 自己的解法 没有任何优化点
     * @param nums
     * @param target
     * @return
     */
    private static int[] twoSum(int[] nums, int target) {
        int[] result = new int[2];
      //先排序
      if (nums != null && nums.length >0){
          //自己和其他的数相加 得出结果
          for (int i = 0; i < nums.length; i++) {
              for (int i1 = 0; i1 < nums.length; i1++) {
                  if (nums[i]+nums[i1] == target){
                      if (i == i1) continue;
                      result[0] = i;
                      result[1] = i1;
                      return result;
                  }
              }
          }
      }
      return result;
    }

    /**
     * 社区解答 一 O(n^2)
     * @param nums
     * @param target
     * @return
     */
    private static int[] twoSumOfFirst(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[j] == target - nums[i]) {
                    return new int[] { i, j };
                }
            }
        }
        throw new IllegalArgumentException("No two sum solution");
    }

    /**
     * 减少时间复杂度O(n)
     * @param nums
     * @param target
     * @return
     */
    private static int[] twoSumOfSecond(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], i);
        }
        for (int i = 0; i < nums.length; i++) {
            int complement = target - nums[i];
            if (map.containsKey(complement) && map.get(complement) != i) {
                return new int[] { i, map.get(complement) };
            }
        }
        throw new IllegalArgumentException("No two sum solution");
    }

    /**
     * 时间复杂度 O(n)
     * @param nums
     * @param target
     * @return
     */
    private static int[] twoSumOfThree(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int complement = target - nums[i];
            if (map.containsKey(complement)) {
                return new int[] { map.get(complement), i };
            }
            map.put(nums[i], i);
        }
        throw new IllegalArgumentException("No two sum solution");
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1,2,3,4,5};
        System.out.println(Arrays.toString(twoSum(nums,7)));
        System.out.println(Arrays.toString(twoSumOfFirst(nums,7)));
        System.out.println(Arrays.toString(twoSumOfSecond(nums,7)));
        System.out.println(Arrays.toString(twoSumOfThree(nums,7)));
    }
}
